import datetime,cgi
import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
from os import environ
from models import *

class BrowseUserActivity(webapp.RequestHandler):

	def post(self):
		self.redirect("/browseuseractivitymain")

	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		admissionnumber=''
		url = self.request.url
		for i in range(0,len(url),1):
			if(url[i]=='?'):
				i=i+1
				while(url[i]!='='):
					admissionnumber=admissionnumber+url[i]
					i=i+1
				break
		users=db.GqlQuery("SELECT * FROM userDetails WHERE admissionnumber='%s'" %admissionnumber)
		for user in users:
			tmp=[]
			tmp.append(user.username)
			currentprojectids=user.projectids
			if(currentprojectids==None):
				self.redirect("/browseuseractivitymain")
				return
			projids=currentprojectids.split(",")
			tmp1=[]
			for pid in projids:
				currentprojects=db.GqlQuery("SELECT * FROM projectDetails WHERE projectid='%s'" %pid)
				for project in currentprojects:
					tmpprojs=project.projectname
				tmp1.append(tmpprojs)
			tmp.append(tmp1)
			break
				
		path = os.path.join(os.path.dirname(__file__),'browseuser.html')#use template
		self.response.out.write(template.render(path,{"details":tmp}))#using template
			

class BrowseUserActivityMain(webapp.RequestHandler):

	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		users=db.GqlQuery("SELECT * FROM userDetails")
		tmp=[]
		for user in users:
			tmp1=[]
			tmp1.append(user.admissionnumber)
			tmp1.append(user.username)
			tmp1.append(user.branch)
			tmp.append(tmp1)
		path = os.path.join(os.path.dirname(__file__),'browseusermain.html')#use template
		self.response.out.write(template.render(path,{"users":tmp}))#using template

application = webapp.WSGIApplication([('/browseuseractivitymain', BrowseUserActivityMain),('/browseuseractivity', BrowseUserActivity)],debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
